﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GymTimer.Models
{
    public  interface ITrain
    {

        public string Name { get; }
        public TimeSpan TotalTime { get; }

        public bool IsCompleted { get;  }


        public bool GoToNext();


        public (string, TimeSpan) CurrentTrainItem();
        public (string, TimeSpan,bool) NextTrainItem();

    }




    public enum TrainStep
    {
        Prepare,
        Training,
        Rest
    }


    public class ExerciseTranin : ITrain
    {
        private readonly Exercise _exercise;
        private TrainStep _step = TrainStep.Training;
        private int _currentRound;


        public bool IsCompleted { get; set; }

        public ExerciseTranin(Exercise exercise)
        {
            _exercise = exercise;
        }

        public string Name => _exercise.Name;


        public bool GoToNext()
        {

            if(IsCompleted)
            {
                return false;
            }

            switch (_step)
            {
                case TrainStep.Training:
                    _step = TrainStep.Rest;
                    return true;


                case TrainStep.Rest:
                    _step = TrainStep.Training;
                    _currentRound++;
                    if(_currentRound == _exercise.Round)
                    {
                        IsCompleted = true;
                        return false;
                    }
                    return true;

                default:
                    return false;
                    
            }
        }


        public TimeSpan TotalTime => TimeSpan.FromSeconds(_exercise.TotalTime());

        public (string, TimeSpan) CurrentTrainItem()
        {

            if (_step == TrainStep.Training)
            {
                return (_exercise.Name, TimeSpan.FromSeconds(_exercise.ExerciseTime));
            }

            if (_step == TrainStep.Rest)
            {
                if(_currentRound==_exercise.Round-1)
                {
                    return ("休息", TimeSpan.FromSeconds(_exercise.LastRestTime));

                }

                return ("休息", TimeSpan.FromSeconds(_exercise.RestTime));
            }
            return ("",TimeSpan.Zero);


        }


        public (string, TimeSpan,bool) NextTrainItem()
        {
            if (_step == TrainStep.Training)
            {
                if (_currentRound == _exercise.Round - 1)
                {
                    return ("休息", TimeSpan.FromSeconds(_exercise.LastRestTime),false);

                }
                return ("休息", TimeSpan.FromSeconds(_exercise.RestTime),false);

                
            }

            if (_step == TrainStep.Rest)
            {

                if (_currentRound == _exercise.Round - 1)
                {
                    return ("", TimeSpan.Zero, true);

                }

                return (_exercise.Name, TimeSpan.FromSeconds(_exercise.ExerciseTime),false);


            }
            return ("", TimeSpan.Zero,true);
        }
    }

    public class ScheduleTranin
    {
        private readonly Schedule _schedule;


        //private readonly Exercise _currentExercise;

        private int _exerciseIndex=0;

        private ExerciseTranin _currentExercise;


        //private int _curRound=0;

        private TrainStep _step=TrainStep.Prepare;

        public ScheduleTranin(Schedule schedule)
        {
            _schedule = schedule;

            _currentExercise = new ExerciseTranin(_schedule.Exercises[_exerciseIndex]);
        }

        public bool IsCompleted { get; set; }


        public string Name {
            get
            {
                return _schedule.Name;
            }
        }
        public TimeSpan TotalTime {
            get
            {
                return  TimeSpan.FromSeconds(_schedule.TotalTime());
            }
        }


        public (string, TimeSpan) CurrentTrainItem()
        {

            if(_step==TrainStep.Prepare)
            {
                if(_schedule.RepareTime>0)
                {
                    return ("准备", TimeSpan.FromSeconds(_schedule.RepareTime));
                }
            }

            return _currentExercise.CurrentTrainItem();

        }
        public (string, TimeSpan,bool) NextTrainItem()
        {
            (string nextName,TimeSpan nextTime,bool complete)=_currentExercise.NextTrainItem();

            if (complete)
            {
                _exerciseIndex++;

                if(_exerciseIndex!= _schedule.Exercises.Count)
                {
                    _currentExercise = new ExerciseTranin(_schedule.Exercises[_exerciseIndex]);
                    return _currentExercise.NextTrainItem() ;
                }else
                {
                    return ("",TimeSpan.Zero,false);
                }
            }
            return (nextName,nextTime,complete);



        }


        public bool GoToNext()
        {

            if (IsCompleted)
            {
                return false;
            }

            switch (_step)
            {
                case TrainStep.Prepare:
                    return true;

            }


            bool ret=_currentExercise.GoToNext();


            if(!ret)
            {
                IsCompleted = false;
            }

            return ret;
        }
    }






}
